
package br.unirio.bsi.dsw.AuctionSite.action;

import org.apache.struts.action.*;

import flexjson.JSONSerializer;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.unirio.bsi.dsw.AuctionSite.beans.formBeanA;
import br.unirio.bsi.dsw.AuctionSite.beans.formBeanE;

import br.unirio.bsi.dsw.AuctionSite.beans.loginForm;
import br.unirio.bsi.dsw.AuctionSite.beans.artistaForm;

import br.unirio.bsi.dsw.AuctionSite.model.dao.GenericDAO;

public class artistaAction extends Action 
{
  @Override
  public ActionForward execute(ActionMapping mapping, 
                               ActionForm form,
                               HttpServletRequest request, 
                               HttpServletResponse response) throws Exception 
  {      
    // pega a session
    HttpSession session = request.getSession();  
    
    // check logged
    loginForm lf = (loginForm)session.getAttribute("loginForm"); 
    if( lf == null || !lf.isLogged() )
      return null;
    
    // pega instancia do DAO
    GenericDAO gD = GenericDAO.getInstance(getResources(request).getMessage("DB_NAME"));
    
    // get form
    artistaForm aF = (artistaForm)form;
    
    
    String search = aF.getSearch();
    if( search != null && !search.equals("") )    
    {
      gD.retrieveAll( "SELECT DISTINCT a.* " +
                      "FROM Artista a, Album b " +
                      "WHERE a.id=b.idartista " +
                      "AND (a.nome ~* '" + search + "') " +              
                      "ORDER BY a.nome ASC " +
                      "OFFSET " + aF.getSelectedPage() +
                      " LIMIT 60", formBeanA.class, aF.getFieldArtista().getArtistas() );
    }
    else
    {      
      gD.retrieveAll( "SELECT DISTINCT a.* " +
                      "FROM Artista a, Album b " +
                      "WHERE a.id=b.idartista " +
                      "ORDER BY a.nome ASC " +
                      "OFFSET " + aF.getSelectedPage() +
                      " LIMIT 60", formBeanA.class, aF.getFieldArtista().getArtistas() );
    }

    // generate json reply    
    String selArt = aF.getSelectedArtista();
    if( selArt != null && !selArt.equals("") )
    {    
      gD.retrieveAll( "SELECT e.* " +
                      "FROM Estilo e, Artista a, EstiloArtista ea " +
                      "WHERE a.id=ea.idartista " +
                      "AND   ea.idestilo=e.id " +
                      "AND   a.id = " + selArt,
                      formBeanE.class, aF.getFieldEstilo().getEstilos() );
    }
           
    String jsonArtista = new JSONSerializer()
            .exclude("class","selectedArtista")
            .serialize( aF.getFieldArtista().getArtistas() );
    
    jsonArtista = "({\"artista\":" + jsonArtista + ",";

    String jsonEstilo = new JSONSerializer()
            .exclude("class","IDSuperEstilo")
            .serialize( aF.getFieldEstilo().getEstilos() );
    
    jsonEstilo = "\"estilo\":" + jsonEstilo + "})";
        
    response.getWriter().write( jsonArtista + jsonEstilo );
    response.getWriter().flush();

    return null;
  }
}
